package com.pirestupppai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.pirestupppai.R;
import com.pirestupppai.imagetool.CustomStickerModel;

import java.util.List;

/**
 *  图片编辑，顶部滑动Adapter
 */

public class ImageEditAdapter extends RecyclerView.Adapter<ImageEditAdapter.MyViewHolder>{
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_LAST = 1;

    private Context mContext;
    private List<CustomStickerModel> mList;
    private LayoutInflater mInflater;
    private ImageEditAdapter.OnItemClickListener mListener;
    private View mLastView;

    private int mCurrentLayerId = 0;          // 当前选中的图层

    public ImageEditAdapter(Context context, List<CustomStickerModel> list) {
        this.mContext = context;
        this.mList = list;
        this.mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getItemViewType(int position) {
        if (mLastView == null) {
            return TYPE_ITEM;
        }
        if (position == mList.size()) {
            return TYPE_LAST;
        }
        return TYPE_ITEM;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mLastView != null && viewType == TYPE_LAST) {
            return new MyViewHolder(mLastView);
        }
        View view = mInflater.inflate(R.layout.item_image_edit, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        if (getItemViewType(position) == TYPE_LAST) {
            return;
        }

        holder.setData(position);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentLayerId = position;
                mListener.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size() + 1;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        RelativeLayout backgroundView;

        MyViewHolder(View itemView) {
            super(itemView);
            if (itemView == mLastView) {
                return;
            }
            imageView = (ImageView) itemView.findViewById(R.id.img_preview);
            backgroundView = (RelativeLayout) itemView.findViewById(R.id.img_background);
        }

        public void setData(int position) {
            CustomStickerModel customStickerModel = mList.get(position);
            imageView.setImageBitmap(customStickerModel.bitmap);
            if (mCurrentLayerId == position) {
                backgroundView.setVisibility(View.VISIBLE);
            } else {
                backgroundView.setVisibility(View.INVISIBLE);
            }
        }
    }

    public void setLastView(View lastView) {
        mLastView = lastView;
    }

    public void setLayerId(int layerId) {
        this.mCurrentLayerId = layerId;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}
